package gruppe7.servlets;

import gruppe7.model.Model;
import gruppe7.model.Station;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SaveStationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String newName = (String) request.getParameter("editedName");
	    String oldName = (String) request.getParameter("oldName");
	    String zoneStr = (String) request.getParameter("editedZone");
	    String manager = (String) request.getParameter("manager");
	    String comment = (String) request.getParameter("comment");
	    String date = (String) request.getParameter("erectiondate");
	    int zone = Integer.parseInt(zoneStr);
		
	    try {
	    	Model model = Model.getInstance();
	    	Station station = model.getStationByName(oldName);
	    	
	    	model.updateStationInfo(station, newName, zone, manager, date, comment);
	  
	    	String url = "/gruppe7/show_specific_station.jsp?station=" + newName;
	    	response.sendRedirect(response.encodeRedirectURL(url));
	    }
	    
	    catch (Exception e) {
	    	response.getWriter().println(e.getMessage());
	    }
	}

}
